# This file was synchronized with template ($Revision: 7 $)
#
# define macros that creates directiries
mkinstalldirs = $(INSTALL_SCRIPT) -d

if MINGW32
    MKTEMP=$(top_srcdir)/config/mktemp.sh
else
    MKTEMP=mktemp
endif

# type in sources here
#BOOTSTRAPSOURCES=lexer.yrd parser.yrd
SOURCES = version.ml iL.ml misc.ml parser.ml lexer.ml\
          genDummy.ml genText.ml dumpHTML.ml genOstap.ml\
          diagnostics.ml namer.ml transformAux.ml transformPredicates.ml\
          expandEBNF.ml expandMeta.ml simplifyGrammar.ml transform.ml\
          genElkTokens.ml genGLR.ml main.ml
MLI =

# folder for temporary object files. 
# must be "./" for current, but not empty string
OBJS_DIR=objs/

SECONDARY_FILES = .depend lexer.ml parser.ml parser.mli parser.output
.SECONDARY: $(SECONDAY_FILES)

# sort sources before use
# (sorting is required for correct link order)
SORTED = $(shell $(OCAMLDSORT) $(SOURCES))

# CMO - all compiled objects
CMO := $(patsubst %.ml, $(OBJS_DIR)%.cmo, $(SORTED))

# all compiled interfaces
CMI := $(CMO:.cmo=.cmi)
# all compiled native objects (OCaml' runtime)
CMX := $(CMO:.cmo=.cmx)
# all compiled native objects (separated)
OBJ := $(CMO:.cmo=.o)

# compiled library
CMA := $(addsuffix .cma, $(PACKAGE))
# compiled native library (OCaml' runtime)
CMXA := $(addsuffix .cmxa, $(PACKAGE))
# compiled native library (separated)
LIB := $(addsuffix .a, $(PACKAGE))
# executable bytecode
BYTE := $(addsuffix .byte$(EXEEXT), $(PACKAGE))
# executable native bytecode
NATIVE := $(addsuffix .opt$(EXEEXT), $(PACKAGE))

# add extra compiled libraries used for linking
#EXTRA_CMA += $(addsuffix .cma, lib1 lib2)
# extra compiled native libraries used for native linking
EXTRA_CMXA := $(EXTRA_CMA:.cma=.cmxa)

# sources for .cma linking
CMO4CMA = $(addsuffix .cmo, version yard)
# sources for .cmxa linking
CMX4CMXA = $(CMO4CMA:.cmo=.cmx)

DOCFILES = $(PACKAGE).ps $(PACKAGE).html

# build target
TARGET := $(BYTE) $(NATIVE) 
# OCaml parser call with flags
AM_P4FLAGS = -pp "$(CAMLP4O) $(PARSER_INCLUDES) pa_log.cmo pa_ru.cmo $(LOG)"
#AM_P4FLAGS = -pp "$(CAMLP4R) $(PARSER_INCLUDES)"

# how to run yard
#YARD := ocamlrun saved/yard

# bytecode compiling flags
AM_BFLAGS = -w Ae $(MODULE_INCLUDES) $(DEBUG)
# native (optimized) compiling flags
AM_OFLAGS = -w p $(MODULE_INCLUDES)

# extra .cmi to delete those are generated from .ml
#CLEAN_CMI := $(CMO:.cmo=.cmi)
# other extra files to clean up
CLEAN_EXTRA := $(BYTE) $(NATIVE) $(SECONDARY_FILES)

# default build:
# - update timestamp
# - build default target
# - build bytecode and optimized native code
all-am: $(TARGET) byte-code native-code

# default clean
clean-am:
	rm -fR $(CMA) $(CMXA) $(CMO) $(CMI) $(CLEAN_CMI) $(CMX) $(OBJ) $(LIB) META $(CLEAN_EXTRA)

@OCAMLFIND_TRUE@install-data-am: $(TARGET)
@OCAMLFIND_TRUE@	if test "$(SITELIBDIR)" != ""; then \
@OCAMLFIND_TRUE@	    $(mkinstalldirs) $(SITELIBDIR); \
@OCAMLFIND_TRUE@	    $(OCAMLFIND) install -destdir $(SITELIBDIR) $(PACKAGE) $(TARGET); \
@OCAMLFIND_TRUE@	else \
@OCAMLFIND_TRUE@	    $(OCAMLFIND) install $(PACKAGE) $(TARGET); \
@OCAMLFIND_TRUE@	fi

@OCAMLFIND_FALSE@install-data-am: $(TARGET)
@OCAMLFIND_FALSE@	if test "$(SITELIBDIR)" = ""; then \
@OCAMLFIND_FALSE@	    installdir=$(OCAMLLIB)/site-lib/$(PACKAGE); \
@OCAMLFIND_FALSE@	else \
@OCAMLFIND_FALSE@	    installdir=$(SITELIBDIR)/$(PACKAGE); \
@OCAMLFIND_FALSE@	fi; \
@OCAMLFIND_FALSE@	$(mkinstalldirs) $$installdir; \
@OCAMLFIND_FALSE@	for i in $(TARGET); do \
@OCAMLFIND_FALSE@	    $(INSTALL_DATA) $$i $$installdir/$$i; \
@OCAMLFIND_FALSE@	done

install-exec-am: $(BYTE) $(NATIVE)
	$(mkinstalldirs) ${bindir}
	$(INSTALL_PROGRAM) $(BYTE) ${bindir}/$(BYTE)
	$(INSTALL_PROGRAM) $(NATIVE) ${bindir}/$(PACKAGE)$(EXEEXT)

uninstall-am:
@OCAMLFIND_TRUE@	if test "$(SITELIBDIR)" = ""; then \
@OCAMLFIND_TRUE@	    $(OCAMLFIND) remove $(PACKAGE); \
@OCAMLFIND_TRUE@	else \
@OCAMLFIND_TRUE@	    rm -fR $(SITELIBDIR)/$(PACKAGE); \
@OCAMLFIND_TRUE@	fi
@OCAMLFIND_FALSE@	if test "$(SITELIBDIR)" = ""; then \
@OCAMLFIND_FALSE@	    rm -fR $(OCAMLLIB)/site-lib/$(PACKAGE); \
@OCAMLFIND_FALSE@	else \
@OCAMLFIND_FALSE@	    rm -fR $(SITELIBDIR)/$(PACKAGE); \
@OCAMLFIND_FALSE@	fi
	rm -f ${bindir}/$(BYTE) ${bindir}/$(PACKAGE)

# clean up docs in distclean mode
distclean-am:
	rm -fR doc

# cleanup autogenerated scripts in maintainer-clean mode
maintainer-clean-generic:
	rm -rf $(top_srcdir)/autom4te.cache $(CLEAN_EXTRA)

# what is byte-code target
.PHONY: byte-code bc
byte-code: $(BYTE)

# how to build bytecode target
$(BYTE): $(CMO)
	$(OCAMLC) $(BFLAGS) -o $@ $(EXTRA_CMA) $(CMO)

# what is native-code target
.PHONY: native-code nc
native-code: $(NATIVE)

# how to build native code target
$(NATIVE): $(CMX)
	$(OCAMLOPT) $(OFLAGS) -o $@ $(EXTRA_CMXA) $(CMX)

# shorter acronims
bc: byte-code
nc: native-code

# how to build library
$(CMA): $(CMO4CMA)
	$(OCAMLC) -a -o $@ $(CMO4CMA)

# how to build native optimized library
$(CMXA): $(CMX4CMXA)
	$(OCAMLOPT) -a -o $@ $(CMX4CMXA)

# every build updates timestamp, so following commit
# will increase revision
.PHONY: update-revision
update-revision: $(top_srcdir)/VERSION
	@umask 022; \
	tmp_version=`$(MKTEMP) VERSION.XXXXXX`; \
	revision=`grep "\\$$Revision:" $< | sed -e 's,^\\$$,,' -e 's, \\$$$$,,'`; \
	date=`LC_TIME=C date`; \
	echo "\$$$$revision \$$" > $$tmp_version; \
	echo "Modify date: $$date" >> $$tmp_version; \
	cat $$tmp_version > $<; \
	rm -f $$tmp_version;

# bootstrapping
#bootstrap: $(BYTE) $(BOOTSTRAPSOURCES)
#	test -d saved || mkdir saved;\
#	tmp=`$(MKTEMP) -d tmp.XXXXXX`;\
#	mv saved $$tmp;\
#	mv $$tmp saved;\
#	mv parser.ml lexer.ml saved/;\
#	./$(BYTE) lexer.yrd -o lexer.ml -gen combi -style lazy2;\
#	./$(BYTE) parser.yrd -o parser.ml -gen combi -style lazy2;\
#	$(MAKE)

restore: 
	cp saved/parser.ml saved/lexer.ml .

pa_yard.cmo: types.cmo p4yard.cmo 
	$(OCAMLC) -pack -o $@ $^ 
# -I +camlp4 pcaml

# META description for package
#META: $(top_srcdir)/VERSION
#	@echo "Generating META description"; \
#	revision=`grep "\\$$Revision:" $< | sed -e 's,^\\$$,,' -e 's, \\$$$$,,'`; \
#	date=`LC_TIME=C date`; \
#	echo "version = \"$$revision\"" > $@; \
#	echo "description = \"Yet Another Recursive Descendent parser genrator\"" >> $@; \
#	echo "requires = \"\"" >> $@; \
#	echo "archive(byte) = \"$(PACKAGE).cma\"" >> $@; \
#	echo "archive(native) = \"$(PACKAGE).cmxa\"" >> $@

# save compiled revision into module
#version.ml: $(top_srcdir)/VERSION
#	@echo "Generating version.ml"; \
#	revision=`grep "\\$$Revision:" $< | sed -e 's,^\\$$,,' -e 's, \\$$$$,,'`; \
#	date=`LC_TIME=C date`; \
#	echo "let version = \"$$revision\"" > $@; \
#	echo "let date = \"$$date\"" >> $@

# resolve compiling dependencies
.depend: $(SOURCES)
	cd ..; ocamldep  $(MODULE_INCLUDES) $(PARSER_INCLUDES) -I src $(patsubst %,src/%,$^) | sed -r "s#(^| )src/#\1$(OBJS_DIR)#g"> src/.depend; cd src 

-include .depend

# generic rules
###############
$(OBJS_DIR)%.cmi: %.mli
	$(OCAMLC) -c $(BFLAGS) $(P4FLAGS)  -I $(OBJS_DIR) -o $@ $<

# Note: cmi <- mli should go first
$(OBJS_DIR)%.cmi: %.ml
	$(OCAMLC) -c $(BFLAGS) $(P4FLAGS)  -I $(OBJS_DIR) -o $@ $<

$(OBJS_DIR)%.cmo: %.ml
	$(OCAMLC) -c $(BFLAGS) $(P4FLAGS)  -I $(OBJS_DIR) -o $@ $<

$(OBJS_DIR)%.cmo: %.ml4r
	$(OCAMLC) -c -pp "$(CAMLP4R) -impl" -I +camlp4 -I $(OBJS_DIR) -o $@ -impl $<

$(OBJS_DIR)%.cmo: %.ml4y
	$(OCAMLC) -c -pp "$(CAMLP4O) ./pa_yard.cmo -impl" -I +camlp4  -I $(OBJS_DIR) -o $@ -impl $<

$(OBJS_DIR)%.o: %.ml
	$(OCAMLOPT) -c $(OFLAGS) $(P4FLAGS)  -I $(OBJS_DIR) -o $@ $< 

$(OBJS_DIR)%.cmx: %.ml
	$(OCAMLOPT) -c $(OFLAGS) $(P4FLAGS)  -I $(OBJS_DIR) -o $@ $<

%.ml: %.mll
	$(OCAMLLEX)  $<

%.output %.ml %.mli: %.mly
	$(OCAMLYACC) -v  $<

%.dvi: %.tex
	latex $< && latex $<

%.ps: %.dvi
	dvips $< -o $@ 

%.html: %.tex
	hevea $<

# make .tex from .ml
$(NAME).tex: $(SOURCES)
	$(OCAMLWEB) -o $@ $^

# yard scripts
#%.ml: %.yrd
#	$(YARD) $< -o $* -gen combi
